package ipad.asia.controller;

import ipad.asia.config.NewEvaluate;
import ipad.asia.entity.Evaluate;
import ipad.asia.entity.Product;
import ipad.asia.service.evaluate.EvaluateFeign;
import ipad.asia.service.product.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EvaluateController {
    @Autowired
    private EvaluateFeign evaluateFeign;
    @Autowired
    private ProductFeign productFeign;
    @RequestMapping("/evaluate/product")
    public String evaluate(@RequestParam(value = "productId",required = false)String productId,
                           HttpSession session,Model model) throws Exception {

        if(productId==null){
            return "";
        }else {
            List<Evaluate> evaluates = evaluateFeign.findByProductId(Integer.parseInt(productId));
            Product product=productFeign.getById(Integer.parseInt(productId));
            model.addAttribute("product",product);
            model.addAttribute("evaluates",evaluates);
            return "evaluate";
        }
    }



    @RequestMapping("/evaluate/all")
    public String evaluateList(Model model, HttpSession session) throws Exception {
            int shopId= (int) session.getAttribute("shopId");
        List<Evaluate> evaluates = evaluateFeign.findByShopId(shopId);
        List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
        List<NewEvaluate> newEvaluates=new ArrayList<>();
        for(int i=0;i<evaluates.size();i++){
            for(int j=0;j<products.size();j++){
                if(evaluates.get(i).getProductId()==products.get(j).getId()){
                      NewEvaluate newEvaluate=new NewEvaluate();
                      newEvaluate.setEvaluate(evaluates.get(i));
                      newEvaluate.setProduct(products.get(i));
                      newEvaluates.add(newEvaluate);
                }
            }
        }
        System.out.println("nnewEvaluates:"+newEvaluates);
            model.addAttribute("newEvaluates",newEvaluates);
            return "evaluateAll";
    }
}
